#include "music.h"




Music::Music():isLike(false),isHistory(false)
{

}

Music::Music(const QUrl &url):isLike(false),isHistory(false),musicUrl(url)
{
    this->musicId=QUuid::createUuid().toString();
    //解析歌曲元数据信息
    //歌曲名称 歌手 专辑 总时长
    this->parse();
}

void Music::setMusicId(const QString &musicId)
{
    this->musicId=musicId;
}

void Music::setMusicName(const QString &musicname)
{
    this->musicName=musicname;
}

void Music::setMusicSinger(const QString &musicsinger)
{
    this->musicSinger=musicsinger;
}

void Music::setMusicCover(const QString &musiccover)
{
    this->musicCover=musiccover;
}

void Music::setDuration(const qint64 &duration)
{
    this->duration=duration;
}

void Music::setIsLike(bool isLike)
{
    this->isLike=isLike;
}

void Music::setIsHistory(bool isHistory)
{
    this->isHistory=isHistory;
}

void Music::setMusicUrl(const QUrl &url)
{
    this->musicUrl=url;
}

QString Music::getMusicName() const
{
    return this->musicName;
}

QString Music::getMusicSinger()const
{
    return this->musicSinger;
}

QString Music::getMusicCover()const
{
    return this->musicCover;
}

qint64 Music::getDuration()const
{
    return this->duration;
}

bool Music::getIsLike()const
{
    return this->isLike;
}

bool Music::getIsHistory()const
{
    return this->isHistory;
}

QUrl Music::getMusicUrl()const
{
    return this->musicUrl;
}

QString Music::getMusicId() const
{
    return this->musicId;
}

QString Music::getLrcFile() const
{
    QString lrcPath=musicUrl.toLocalFile();


    lrcPath.replace(".mp3",".lrc");
    lrcPath.replace(".flac",".lrc");
    lrcPath.replace(".wav",".lrc");

    return lrcPath;
}

void Music::insertMusicToDB()
{
    //1.判断music是否已存在数据库中
    QSqlQuery query;
    query.prepare("SELECT EXISTS(SELECT 1 FROM musicInfo WHERE musicId=?)");
    query.addBindValue(musicId);
    if(!query.exec())
    {
        qDebug()<<"查询失败:"<<query.lastError().text();
        return;
    }
    //2.存在，更新isLike和isHistory
    //3.不存在，插入music属性信息
    if(query.next())
    {
        bool isExists=query.value(0).toBool();
        if(isExists)
        {
            query.prepare("UPDATE musicInfo SET isLike=?,isHistory=? WHERE musicId=?");
            query.bindValue(0,isLike?1:0);
            query.bindValue(1,isHistory?1:0);
            query.bindValue(2,musicId);
            if(!query.exec())
            {
                qDebug()<<"更新歌曲:"<<musicName<<" 失败:"<<query.lastError().text();
                return;
            }
            qDebug()<<"更新成功:"<<musicName;
        }
        else
        {
            query.prepare("INSERT INTO musicInfo(musicId,musicName,musicSinger,musicCover,musicUrl,duration,isLike,isHistory)\
                          VALUES(?,?,?,?,?,?,?,?)");
            query.bindValue(0,musicId);
            query.bindValue(1,musicName);
            query.bindValue(2,musicSinger);
            query.bindValue(3,musicCover);
            query.bindValue(4,musicUrl.toLocalFile());
            query.bindValue(5,duration);
            query.bindValue(6,isLike?1:0);
            query.bindValue(7,isHistory?1:0);
            if(!query.exec())
            {
                qDebug()<<"写入歌曲:"<<musicName<<" 失败:"<<query.lastError().text();
                return;
            }
            qDebug()<<"写入歌曲成功:"<<musicName;
        }
    }
}

void Music::parse()
{
    QMediaPlayer player;
    player.setMedia(this->musicUrl);

    while(!player.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }

    if(player.isMetaDataAvailable())
    {
        this->musicName=player.metaData("Title").toString();
        this->musicSinger=player.metaData("Author").toString();
        this->musicCover=player.metaData("AlbumTitle").toString();
        this->duration=player.metaData("Duration").toLongLong();

        QString filename=this->musicUrl.fileName();
        int index=filename.indexOf('-');
        int indexPos=filename.indexOf('.');

        //如果musicName为空
        if(this->musicName=="")
        {
            if(index!=-1)  //找到了
            {
                this->musicName=filename.mid(0,index).trimmed();
            }
            else
            {
                this->musicName=filename.mid(0,indexPos).trimmed();
            }
        }
        //如果musicSinger为空
        if(this->musicSinger=="")
        {
            if(index!=-1)
            {
                this->musicSinger=filename.mid(index+1,indexPos-index-1).trimmed();
            }
            else
                this->musicSinger="未知歌手";
        }
        //如果musicCover为空
        if(this->musicCover=="")
        {
           this->musicCover="未知专辑";
        }

        //qDebug()<<musicName<<":"<<musicSinger<<":"<<musicCover<<":"<<duration;
    }
}
































